package xyz.msj.hrm.web;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import xyz.msj.hrm.properties.RabbitMQConfig;
import xyz.msj.hrm.util.AjaxResult;
import xyz.msj.hrm.vo.SendEmailVO;
import xyz.msj.hrm.vo.SendSMSVO;
import xyz.msj.hrm.vo.SendSystemMessageVO;

/**
 * mq控制层
 */
@RestController
@Slf4j
public class MQController {
    //mq模板
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送短信
     * @return
     */
    @PostMapping("send/sendSMS")
    public AjaxResult sendSMS(@RequestBody SendSMSVO vo){
        log.debug("发送短信到队列,{}" + vo.toString());
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.EXCHANGE_NAME_TOPIC,
                RabbitMQConfig.ROUTING_KEY_SMS,
                JSON.toJSONString(vo)
        );
        return AjaxResult.me();
    }
    /**
     * 发送邮件
     * @return
     */
    @PostMapping("send/sendEmail")
    public AjaxResult sendEmail(@RequestBody SendEmailVO vo){
        log.debug("发送邮件到队列,{}" + vo.toString());
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.EXCHANGE_NAME_TOPIC,
                RabbitMQConfig.ROUTING_KEY_EMAIL,
                JSON.toJSONString(vo)
        );
        return AjaxResult.me();
    }
    /**
     * 发送系统消息
     * @return
     */
    @PostMapping("send/sendSystemMessage")
    public AjaxResult sendSystemMessage(@RequestBody SendSystemMessageVO vo){
        log.debug("发送系统消息到队列,{}" + vo.toString());
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.EXCHANGE_NAME_TOPIC,
                RabbitMQConfig.ROUTING_KEY_SYSTEM_MESSAGE,
                JSON.toJSONString(vo)
        );
        return AjaxResult.me();
    }

}
